class InterpolationEvaluationException(KeyError):
    pass 

class jsonCfg(dict):
    # set default value for any key
    defaultValue = {'user_agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0',
                    'gap_check_second': 60,
                    'in': '{0}',
                    'in_is_link': False,
                    'out': '\\1',
                    'repeat': False,
                    'dest': 1,
                    'cs': False,
                    'html_entities': True,
                    'noclean': False,
                    'clear': False,
                    'trig': {'type': 'regex', 'justmached': None},
                    'notify': {'type': 'gmail'}}

    # open an load json file config
    def open(self, filePath):
        import logging
        import json
        
        f = open(filePath)
        data = f.read()
        f.close()
        
        dictCfg = json.loads(data)
        dict.__init__(self,jsonCfg(dictCfg))
        return dictCfg

    # evalutate item
    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            try:
                return eval(key, self)
            except Exception as e:
                raise InterpolationEvaluationException(key, e)
    
    # get var with key, set default for predefinited key (defaultValue), try to evalutate it 
    def evalGet(self, dictVar, key, parentKey=None):
        # if present in list of default value get with option default
        if(key in self.defaultValue or parentKey in self.defaultValue):
            if(parentKey is None):
                default = self.defaultValue[key]
            else:
                default = self.defaultValue[parentKey][key]
                
            value = dictVar.get(key, default)
        else:
            value = dictVar.get(key)

        # check if str...and try to evalutate (thanks to __getitem__)
        if(isinstance(value, str)):
            value = (value % self)
            
        return value 